package com.frontop.constan;

import com.alibaba.fastjson.JSONArray;
import com.frontop.common.constant.ResponseStatusCodeConstant;
import com.frontop.common.util.ResultJsonUtil;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: YangJia
 * @Desc: 全局处理抛出的异常
 * @Version 1.0
 */
@ControllerAdvice
public class Oauth2ExceptionHandler {

    /**
     * 用户名和密码错误
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(InvalidGrantException.class)
    public ResultJsonUtil<Object> handleInvalidGrantException(InvalidGrantException e) {
        return new ResultJsonUtil(0,ResponseStatusCodeConstant.PWD_ERROR,e.getMessage(),null);
    }

    /**
     * 账户异常(禁用、锁定、过期)
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler({InternalAuthenticationServiceException.class})
    public ResultJsonUtil<Object> handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {
        return new ResultJsonUtil(0,ResponseStatusCodeConstant.USER_LOCK,e.getMessage(),null);
    }


}